package org.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.DataInputStream;
import java.io.IOException;

public class Demo03ReadFile {
    public static void main(String[] args) {
        String server = "hdfs://192.168.56.102:8020";
        String filePath = "/test/a.txt"; // 文件在 HDFS 中的路径

        System.setProperty("HADOOP_USER_NAME", "hadoop");

        Configuration config = new Configuration();
        config.set("fs.defaultFS", server);

        try (FileSystem fs = FileSystem.get(config);
             DataInputStream in = fs.open(new Path(filePath))) {

            byte[] bs = new byte[1024];
            int len;
            while ((len = in.read(bs)) != -1) {
                String str = new String(bs, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}